﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;
using NIIT.SaoBacDau.Aupi.HospitalManagement.BusinessLogic;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Languages;

namespace NIIT.SaoBacDau.Aupi.HospitalManagement
{
    public partial class LoginSystem : Form
    {
        public Boolean bln_Languages { get; set; }

        public Boolean bln_RememberPassword { get; set; }

        public LoginSystem()
        {
            InitializeComponent();
        }

        private void ControlLanguages()
        {
            if (bln_Languages == true)
            {
                tsmi_Languages.Text = "Languages";
                mi_English.Text = "English";
                mi_Vietnamese.Text = "Vietnamese";
                lbl_HospitalManagement.Text = "Hospital Management";
                cbx_RememberPassword.Text = "Remember Password";
            }
            else if (bln_Languages == false)
            {
                tsmi_Languages.Text = "Ngôn ngữ";
                mi_English.Text = "Tiếng Anh";
                mi_Vietnamese.Text = "Tiếng Việt";
                lbl_HospitalManagement.Text = "Quản Lý Bệnh Viện";
                cbx_RememberPassword.Text = "Ghi nhớ tài khoản và mật khẩu";
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            bln_Languages = true;
            bln_RememberPassword = false;
            tbx_UserName.Focus();
        }

        private void bt_Login_Click(object sender, EventArgs e)
        {
            LoginHM();
            
        }

        private void LoginHM()
        {
            Obj_Login Ac = new Obj_Login();
            Ac.UserName = tbx_UserName.Text.ToString();
            Ac.Password = tbx_Password.Text.ToString();
            if (bln_RememberPassword == true)
            {
                CheckAccount(Ac);
            }
            else
            {
                CheckAccount(Ac);
            }
        }

        private void CheckAccount(Obj_Login Ac)
        {
            LoginError Error = BL_Logins.Login(Ac);
            switch (Error)
            {
                case LoginError.FormatUserName:
                    Languages.LibraryLanguages.UserNameIsNotFormat(bln_Languages);
                    tbx_UserName.Text = "";
                    tbx_UserName.Focus();
                    break;
                case LoginError.PasswordIsEmpty:
                    Languages.LibraryLanguages.PasswordIsEmpty(bln_Languages);
                    tbx_Password.Text = "";
                    tbx_Password.Focus();
                    break;
                case LoginError.FormatPassword:
                    Languages.LibraryLanguages.PasswordIsNotFormat(bln_Languages);
                    tbx_Password.Text = "";
                    tbx_Password.Focus();
                    break;
                case LoginError.NotAccount:
                    Languages.LibraryLanguages.NotAccount(bln_Languages);
                    tbx_UserName.Text = "";
                    tbx_Password.Text = "";
                    tbx_UserName.Focus();
                    break;
                case LoginError.NoError:
                    this.Hide();
                    Managements HM = new Managements(BL_Logins.WellComeEmployee(Ac),bln_RememberPassword);
                    HM.ShowDialog();
                    break;
                default:
                    break;

            }
        }

        private void bt_Reset_Click(object sender, EventArgs e)
        {
            tbx_UserName.Text = null;
            tbx_Password.Text = null;
            cbx_RememberPassword.Checked = false;
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            Languages.LibraryLanguages.ExitLogin(bln_Languages);
        }

        private void mi_English_Click(object sender, EventArgs e)
        {
            mi_English.Checked = true;
            mi_Vietnamese.Checked = false;
            bln_Languages = true;
            ControlLanguages();
        }

        private void mi_Vietnamese_Click(object sender, EventArgs e)
        {
            mi_English.Checked = false;
            mi_Vietnamese.Checked = true;
            bln_Languages = false;
            ControlLanguages();
        }

        private void cbx_RememberPassword_CheckedChanged(object sender, EventArgs e)
        {
            bln_RememberPassword = true;
        }

        private void tbx_UserName_KeyUp(object sender, KeyEventArgs e)
        {
            if (tbx_UserName.Text.Length > 0)
                btn_Login.Enabled = true;
            else
                btn_Login.Enabled = false;
        }

        private void tbx_Password_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                LoginHM();
        }
    }
}
